package com.fyzk.activity.special;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.fyzk.base.BaseActivity;
import com.fyzk.data.preference.MySharedPreferences;
import com.tyjc.lottery.R;
import com.fyzk.utils.LogUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import com.wf.domain.UrlDef;

/**
 * Created by fanxs11 on 2017/8/25.
 * 比分直播
 */
@ContentView(R.layout.ac_scorlivestream)
public class ScoreLiveStreamActivity extends BaseActivity {

    @ViewInject(R.id.livestream_webview)
    private WebView webView;
    @ViewInject(R.id.livestream_progress)
    private ProgressBar progressbar;

    private String url = UrlDef.followUrl + "html/zhibo/index.html";

    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    progressbar.setVisibility(View.GONE);
                } else {
                    if (progressbar.getVisibility() == View.GONE) {
                        progressbar.setVisibility(View.VISIBLE);
                    }
                    progressbar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setSaveFormData(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setGeolocationEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        // 设置setWebChromeClient对象
        webView.setWebViewClient(new MyWebViewClient());
        webView.addJavascriptInterface(this, "goback");
        if(MySharedPreferences.getInstance().getLoginStatus()){
            webView.loadUrl(url+"?tk="+MySharedPreferences.getInstance().getAuthtoken()+"&appopen=1");
        }else{
            webView.loadUrl(url);
        }

    }

    private class MyWebViewClient extends WebViewClient {


        // 在WebView中而不是默认浏览器中显示页面
        public boolean shouldOverrideUrlLoading(final WebView view, String url) {
            LogUtil.d("url:"+url);
            if (url.equals(UrlDef.followUrl)) {
                finish();
            } else {
                view.loadUrl(url);
            }

            return true;
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }

        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @SuppressLint("NewApi")
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view,
                                                          String url) {
            // TODO Auto-generated method stub
            WebResourceResponse response = super.shouldInterceptRequest(view, url);
            return response;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
        }

    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @JavascriptInterface
    public void appBack() {
        finish();
    }
}
